<template>
  <!-- 表单 -->
  <el-form ref="employeeForm" :model="employeeForm" :rules="rules" label-width="120px">
    <el-form-item label="姓名" prop="username">
      <el-input v-model="employeeForm.username" style="width:50%" placeholder="请输入姓名" />
    </el-form-item>
    <el-form-item label="手机" prop="mobile">
      <el-input v-model="employeeForm.mobile" style="width:50%" placeholder="请输入手机号" />
    </el-form-item>
    <el-form-item label="入职时间" prop="timeOfEntry">
      <el-date-picker v-model="employeeForm.timeOfEntry" style="width:50%" placeholder="请选择入职时间" />
    </el-form-item>
    <el-form-item label="聘用形式" prop="formOfEmployment">
      <el-select v-model="employeeForm.formOfEmployment" style="width:50%" placeholder="请选择">
        <el-option v-for="item in employees" :key="item.id" :label="item.value" :value="item.id" />
      </el-select>
    </el-form-item>
    <el-form-item label="工号" prop="workNumber">
      <el-input v-model="employeeForm.workNumber" style="width:50%" placeholder="请输入工号" />
    </el-form-item>
    <el-form-item label="部门" prop="departmentName">
      <el-input v-model="employeeForm.departmentName" style="width:50%" placeholder="请选择部门" @focus="departmentNameFocus" @blur="blur" />
      <div v-if="treeShow">
        <el-tree :data="departmentsList" :props="{label: 'name'}" @node-click="handleNodeClick" />
      </div>
    </el-form-item>
    <el-form-item label="转正时间" prop="correctionTime">
      <el-date-picker v-model="employeeForm.correctionTime" style="width:50%" placeholder="请选择转正时间" />
    </el-form-item>
    <el-form-item>
      <el-button @click="addCancel">取消</el-button>
      <el-button type="primary" @click="addSubmit">确定</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import EmployeeEnum from '@/constant/employees'// 导入枚举方法
import { addEmployeeAPI } from '@/api/employees'// 导入API方法
import { getDepartmentsAPI } from '@/api/departments'
import { tranListToTreeData } from '@/utils'// 导入转换树结构的方法
import { constformatTime } from '@/utils' // 导入时间工具
export default {
  name: 'EmpDialog',
  data() {
    return {
      employeeForm: { // 表单数据
        username: '', // 用户名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      employees: EmployeeEnum.hireType, // 部门
      departmentsList: [], // 树形部门列表
      treeShow: false, // 树组件的显示状态
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: 'blur' },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: 'change' }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: 'blur' }
        ],
        correctionTime: [
          { required: true, message: '请选择入职时间', trigger: 'blur' }
        ]
      }
    }
  },

  methods: {
    // 弹框取消
    addCancel() {
      // 给父组件传值
      this.$emit('close')
      // 重置表单
      this.$refs.employeeForm.resetFields()
      // 重置验证
      // this.$refs.employeeForm.clearValidate()
    },

    // 弹框确定
    addSubmit() {
      // 兜底验证
      this.$refs.employeeForm.validate(async valid => {
        // 判断vaild
        if (!valid) return

        // 调用添加员工的API方法
        const res = await addEmployeeAPI(this.employeeForm)
        // 提示成功
        this.$message.success(res.message)
        this.timeOfEntry = constformatTime(this.timeOfEntry) // 调用时间工具,改变时间格式
        this.correctionTime = constformatTime(this.correctionTime)
        // 给父组件传值
        this.$emit('update-empolyee')
        // 给父组件传值
        this.$emit('close')
        // 重置表单
        this.$refs.employeeForm.resetFields()
      })
    },

    // 添加员工
    // async addEmployee() {

    // },

    // 获取部门列表
    async getDepartments() {
      // 调用部门列表的API方法
      const res = await getDepartmentsAPI()
      // 调用转换树结构的方法
      const res2 = tranListToTreeData(res.data.depts, '')
      // 将返回的数据赋值给departmentsList
      this.departmentsList = res2
    },

    // 获取焦点
    departmentNameFocus() {
      // 将显示改为true
      this.treeShow = true
      // 调用获取部门列表的方法
      this.getDepartments()
    },

    // 失去焦点
    blur() {
      setTimeout(() => {
        this.treeShow = false
      }, 1000)
    },

    // 获取点击的部门
    handleNodeClick(data) {
      // 判断是否有子元素
      if (data && data.children) return
      // 将回调参数赋值给departmentName
      this.employeeForm.departmentName = data.name
      // 将显示改为false
      this.treeShow = false
    }
  }
}
</script>

<style>

</style>
